package com.onluy.frameworkcloud.base;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.Param.IPagination;
import com.onluy.frameworkcloud.data.util.InParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public abstract class AppController <T>{
    protected Logger log =   LoggerFactory.getLogger(this.getClass());


    public   abstract AppService  service();


    @Autowired
    protected HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;



    protected RspInfo Add(T t) {
        RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		log.info("------------>add."+t.getClass().getName()+" : "+JSON.toJSONString(t)  );
		int num=0;
		try {
			num=service().add( t);
		} catch (Exception e) {
			jsonResult.setRspDesc("增加异常:"+e.getMessage());
			e.printStackTrace();
		}
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("增加成功！");
		}
		else {
			jsonResult.setRspDesc("增加失败！");
		}
		return jsonResult;
	}

    protected RspInfo Update( T t) {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
        log.info("------------>Update."+t.getClass().getName()+" : "+JSON.toJSONString(t)  );
		int num=1;
		try {
			num=service().update( t);
		} catch (Exception e) {
			jsonResult.setRspDesc("修改异常:"+e.getMessage());
			e.printStackTrace();
		}
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("修改成功！");
		}
		else {
			jsonResult.setRspDesc("修改失败！");
		}
		return jsonResult;
	}


    protected RspInfo Model( T t) {
		RspInfo<T> jsonResult=new RspInfo<T>();
        log.info("------------>Model."+t.getClass().getName()+" : "+JSON.toJSONString(t)  );
        T model=null ;
        try {
            model= (T) service().model( t);
			jsonResult.setRspCode(0);
			jsonResult.setRspData(model);
        } catch (Exception e) {
            model=null ;
			jsonResult.setRspCode(-1);
            e.printStackTrace();
        }
        return jsonResult;
    }

    protected RspInfo Del( T t) {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
        log.info("------------>Del."+t.getClass().getName()+" : "+JSON.toJSONString(t)  );
		int num=1;
		try {
			num=service().del(  t);
		} catch (Exception e) {
			jsonResult.setRspDesc("删除异常:"+e.getMessage());
			e.printStackTrace();
		}
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("删除成功！");
		}
		else {
			jsonResult.setRspDesc("删除失败！");
		}
		return jsonResult;
	}


    protected RspInfo<PageInfo<T>>   List(T t, IPagination pagination ) {
		log.info("------------>List."+t.getClass().getName()+" : "+JSON.toJSONString(t)  );
		RspInfo<PageInfo<T>> jsonResult=new RspInfo<PageInfo<T>>();
		PageInfo<T> list =service().list(t,pagination);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}

    protected RspInfo<List<T>> List( T t) {
		log.info("------------>List."+t.getClass().getName()+" : "+JSON.toJSONString(t)  );
		RspInfo<List<T>> jsonResult=new RspInfo<List<T>>();
        List<T> list =service().list(t);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
        return jsonResult;
    }

    protected RspInfo<PageInfo<T>> simplelist(T t, IPagination pagination ) {
		log.info("------------>List."+t.getClass().getName()+" : "+JSON.toJSONString(t)  );
		RspInfo<PageInfo<T>> jsonResult=new RspInfo<PageInfo<T>>();
        PageInfo<T> list =service().simplelist(t,pagination);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
    }

    protected RspInfo<List<T>> simplelist( T t) {
		log.info("------------>List."+t.getClass().getName()+" : "+JSON.toJSONString(t)  );
		RspInfo<List<T>> jsonResult=new RspInfo<List<T>>();
        List<T> list =service().simplelist(t);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
    }

}
